package cn.jji8.QSGUI;


import cn.jji8.QSGUI.Gui.GuiConfigLoad;
import cn.jji8.QSGUI.Gui.GuiManger;
import cn.jji8.QSGUI.quick.cStats;
import cn.jji8.QSGUI.quick.command.CommandManger;
import cn.jji8.QSGUI.quick.money.Money;
import cn.jji8.QSGUI.quick.tool.Number;
import org.bukkit.Bukkit;
import org.bukkit.plugin.java.JavaPlugin;
import cn.jji8.QSGUI.quick.logger.*;

import java.io.*;
import java.util.logging.Level;


/**
 * 一个主类
 * */
public class Main extends JavaPlugin {
    /**
     * 插件启动时会被调用
     * */
    @Override
    public void onEnable() {
        Logger.load(this);
        Logger.putInfo("插件开始加载...");
        Number.load(this);
        Language.load(this);
        CommandManger.load(this,"QSGUI");
        Bukkit.getPluginManager().registerEvents(new Listener(),this);
        GuiManger.load(this);
        GuiConfigLoad.load(this);
        Money.setupEconomy(this);
        CommandLoad.load(this);
        cStats metrics = new cStats(this);
        Logger.putInfo("插件加载完成。");
    }
    /**
     * 插件关闭时调用
     * */
    @Override
    public void onDisable(){
        getLogger().info("正在保存数据..");
        getLogger().info("保存完毕。");
    }



    /**
     * 用于释放配置文件的方法
     * */
    public void saveResource(String resourcePath, boolean replace) {
        if (resourcePath != null && !resourcePath.equals("")) {
            resourcePath = resourcePath.replace('\\', '/');
            InputStream in = this.getResource(resourcePath);
            if (in == null) {
                throw new IllegalArgumentException("The embedded resource '" + resourcePath + "' cannot be found in " + super.getFile());
            } else {
                File outFile = new File(super.getDataFolder(), resourcePath);
                int lastIndex = resourcePath.lastIndexOf(47);
                File outDir = new File(super.getDataFolder(), resourcePath.substring(0, lastIndex >= 0 ? lastIndex : 0));
                if (!outDir.exists()) {
                    outDir.mkdirs();
                }

                try {
                    if (outFile.exists() && !replace) {
                        //super.getLogger().log(Level.WARNING, "Could not save " + outFile.getName() + " to " + outFile + " because " + outFile.getName() + " already exists.");
                    } else {
                        OutputStream out = new FileOutputStream(outFile);
                        byte[] buf = new byte[1024];

                        int len;
                        while((len = in.read(buf)) > 0) {
                            out.write(buf, 0, len);
                        }

                        out.close();
                        in.close();
                    }
                } catch (IOException var10) {
                    super.getLogger().log(Level.SEVERE, "Could not save " + outFile.getName() + " to " + outFile, var10);
                }

            }
        } else {
            throw new IllegalArgumentException("ResourcePath cannot be null or empty");
        }
    }
}
